package com.ctsi.security.service;

import com.ctsi.commons.event.BaseEvent;
import com.ctsi.commons.event.EventPublisherMan;
import com.ctsi.commons.util.UtilMisc;
import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.security.domain.DataObject;
import com.ctsi.security.domain.DataObjectType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.*;

/**
 * 实现对数据对象类型的CRUD操作
 *
 * @author zhangjw
 *
 */
@Repository
public class DataObjectTypeService {

    @Autowired
	private MybatisGenericDao dao;
    @Autowired
	private DataObjectService dataObjectService;

	/**
	 * gyz added at 2008-4-1 发布事件
	 */
	@Autowired
	private EventPublisherMan publisherMan;

	public void setPublisherMan(EventPublisherMan publisherMan) {
		this.publisherMan = publisherMan;
	}


	/**
	 * 保存数据对象类型数据
	 *
	 * @param dataObjectType
	 */
	public void saveDataObjectType(DataObjectType dataObjectType) {
		Assert.notNull(dataObjectType);
		String id=dataObjectType.getId();
		if(UtilValidate.isEmpty(id)) {
			dataObjectType.setId(UUID.randomUUID().toString());
		}
		
		dao.insert(dataObjectType);
//		dao.saveObject(dataObjectType);
	}

	/**
	 * 修改数据对象类型数据
	 *
	 * @param dataObjectType
	 */
	public void updateDataObjectType(DataObjectType dataObjectType) {
//		dao.updateObject(dataObjectType);
		dao.update(dataObjectType);
		/**
		 * 发布更新缓存事件
		 * gyz modified at 2008-4-1
		 */
		Map<String,Object> eventMap= new HashMap<>();//事件MAP
		eventMap.put("type", "dataObjectTypeId");
		eventMap.put("dataObjectTypeId", UtilMisc.toList(dataObjectType.getDataObjectTypeId()));
		eventMap.put("sourceDoTypeId", dataObjectType.getDataObjectTypeId());
		BaseEvent event=new BaseEvent(dataObjectType,"securityPermissionDo",eventMap);
		publisherMan.getEventPublisher().publishEvent(event);
	}

	/**
	 * 查询全部数据对象类型数据
	 *
	 * @return
	 */
	public List<DataObjectType> findDataObjectTypeAll() {
		
//		String hql = " from DataObjectType";
//		return dao.findAll(hql);
		return dao.find(DataObjectType.class, (Map)null);
	}

	/**
	 * 条件查询数据对象类型
	 *
	 * @param field
	 * @param args
	 * @return
	 */
	public List<DataObjectType> findDataObjectType(String[] field, Object[] args) {

		if(field==null||args==null) {
			return new ArrayList<DataObjectType>(0);
		}
		Assert.isTrue(field.length==args.length,"field's length is not equal args's length");
		Map<String,Object> params=new HashMap<String, Object>();
		for(int i=0;i<field.length;i++) {
			String itemField=field[i];
			Object itemObject=args[i];
			params.put(itemField.toLowerCase(), itemObject);
		}
		return dao.find(DataObjectType.class,params);
	}
	
	public DataObjectType findById(String id){
		String [] ids = {"ID"};
		String [] value = {id};
		List<DataObjectType> list = findDataObjectType(ids,value);
		if(list.size()==1){
			return list.get(0);
		}else{
			return null;
		}		
	}
	public DataObjectType findByTypeId(String id){
		String [] ids = {"DATAOBJECTTYPEID"};
		String [] value = {id};
		List<DataObjectType> list = findDataObjectType(ids,value);
		if(list.size()==1){
			return list.get(0);
		}else{
			return null;
		}		
	}


	public Page<DataObjectType> queryDataObjectType(int pageSize, int startIndex) {
		return dao.queryPage(DataObjectType.class.getName()+".selectByMap", new HashMap<String,Object>(), startIndex, pageSize);

	}
	public Page<DataObjectType> queryDataObjectType(int pageSize, int startIndex,Map map) {
		if(UtilValidate.isEmpty(map)){
			map = new HashMap<String,Object>(0);
		}
		return dao.queryPage(DataObjectType.class.getName()+".selectByMap",map , startIndex, pageSize);

	}

	/**
	 * wangbf add on 2011-10-13
	 * @param pageSize
	 * @param startIndex
	 * @param map
	 * @param permisionIdList
	 * @return
	 */
	public Page<DataObjectType> queryDataObjectType(int pageSize, int startIndex,Map map,List<String> permisionIdList){
		if(UtilValidate.isEmpty(map)){
			map = new HashMap<String,Object>(0);
		}
		
		if(UtilValidate.isNotEmpty(permisionIdList)){
			map.put("secretTypeIds", permisionIdList);
			
		}else{
			map.put("secretTypeIdnull", "true");
		}
		return dao.queryPage(DataObjectType.class.getName()+".selectByMap",map , startIndex, pageSize);
	}

	/**
	 * 删除数据对象类型
	 *
	 * @param id
	 */
	public void removeDataObjectType(String id) {
		DataObjectType dataObjectType=dao.get(DataObjectType.class, id);
		DataObjectType dot=new DataObjectType();
		dot.setId(id);
		dao.remove(dot);
//		dao.removeObject(com.gentlesoft.security.domain.DataObjectType.class,
//				id);
		//发布事件
	Map<String,Object> eventMap=new HashMap<String, Object>();//事件MAP
	eventMap.put("type", "dataObjectTypeId");
	eventMap.put("dataObjectTypeId",UtilMisc.toList(dataObjectType.getDataObjectTypeId()));
	eventMap.put("sourceDoTypeId", dataObjectType.getDataObjectTypeId());
	BaseEvent event=new BaseEvent(dataObjectType,"securityPermissionDo",eventMap);
	publisherMan.getEventPublisher().publishEvent(event);
	}

	/**
	 * 删除数据对象类型,先检查是否有数据对象
	 * @param arrayId
	 * @param arrayDoTypeId
	 * @return
	 */
	public int removeDataObjectType(String[] arrayId,String[] arrayDoTypeId) {
		for (int i = 0; i < arrayId.length; i++) {
			List<DataObject> listDataObject=dataObjectService.findDataObject(new String[]{"dataObjectTypeId"}, new Object[]{arrayDoTypeId[i]});
			if(listDataObject.size()>0) return i;
			this.removeDataObjectType(arrayId[i]);
			//查询是否有功能类型数据对象对应关系有则删除
		}
		return -1;
	}

	// 得到数据对象类型
	public String findSelectDoType() {
		StringBuilder sb = new StringBuilder();
//		String hql = "from com.gentlesoft.security.domain.DataObjectType";
		List<DataObjectType> listDoType = this.findDataObjectTypeAll();
		Iterator<DataObjectType> iteDoType = listDoType.iterator();
		DataObjectType doType = new DataObjectType();
		for (int i = 0; iteDoType.hasNext(); i++) {
			doType = iteDoType.next();
			if (i == 0) {
				sb.append("['" + doType.getDataObjectTypeId() + "','"
						+ doType.getDataObjectTypeName() + "']");
			} else {
				sb.append(",['" + doType.getDataObjectTypeId() + "','"
						+ doType.getDataObjectTypeName() + "']");
			}
		}
		return sb.toString();
	}
	public boolean valDataObjectTypeId(String doTypeId){

		Map<String, Object> params=new HashMap<String, Object>();
		params.put("dataobjecttypeid", doTypeId);
		List<DataObjectType> listDoType = dao.find(DataObjectType.class, params);
		if(listDoType.size()>0){
			return true;
		}else{
			return false;
		}
	}

	public DataObjectService getDataObjectService() {
		return dataObjectService;
	}

	public void setDataObjectService(DataObjectService dataObjectService) {
		this.dataObjectService = dataObjectService;
	}


 


}
